package cn.pink.logicsrv.mod.group;

import cn.pink.common.entity.logic.group.CacheEntityGroup;
import cn.pink.common.support.tools.cache.CacheTool;
import cn.pink.core.NodeType;
import cn.pink.core.Port;
import cn.pink.core.Service;
import cn.pink.core.config.CacheConfig;
import cn.pink.core.gen.lock.Lock;
import cn.pink.core.gen.proxy.DistrClass;
import cn.pink.core.gen.proxy.DistrMethod;
import cn.pink.core.support.Param;
import cn.pink.logicsrv.RPCProxy;
import io.vertx.core.Future;
import io.vertx.core.Promise;

/**
 * 玩家群
 */
@DistrClass(nodeType = NodeType.LOGIC)
public class GroupService extends Service {
    public GroupService(Port port) {
        super(port);
    }

    @Override
    public Object getId() {
        return RPCProxy.GroupServiceProxy.DIST_NAME;
    }

    /**
     * 获取玩家群列表
     */
    @DistrMethod
    public Param getGroupList() {
        return null;
    }

    /**
     * 创建玩家群
     */
    @DistrMethod
    @Lock
    public Param createGroup(String humanId, String name, String notice, int headId, int apply, int requireLv) {
        long groupId = port.applySeqId();

        CacheEntityGroup entityGroup = new CacheEntityGroup();
        entityGroup.setId(groupId);
        entityGroup.setName(name);
        entityGroup.setNotice(notice);
        entityGroup.setHeadId(headId);
        entityGroup.setApply(apply);
        entityGroup.setRequireLv(requireLv);
        entityGroup.setLeader(humanId);

        CacheTool.getInstance().getCache(CacheConfig.GROUP).put(String.valueOf(entityGroup.getId()), entityGroup);

        return new Param(groupId);
    }

    /**
     * 加入玩家群
     */
    @DistrMethod(isReturnAsync = true)
    @Lock
    public Future<Param> joinGroup(String humanId, long groupId) {
        Promise<Param> promise = Promise.promise();

        port.getVertx().setTimer(15000, r -> {
//            promise.complete(new Param().put("stsss", true));
            promise.fail("1111111111111111");
        });

        return promise.future();
    }
}
